package com.atguigu.controller;

import com.atguigu.base.BaseController;
import com.atguigu.entity.Admin;
import com.atguigu.entity.Role;
import com.atguigu.service.AdminRoleService;
import com.atguigu.service.AdminService;
import com.atguigu.service.RoleService;
import com.atguigu.util.QiniuUtils;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author:hx
 * @DateTime:2022/9/23 17:52
 * @Description:
 */
@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {
    @DubboReference
    private AdminService adminService;
    @DubboReference
    private RoleService roleService;
    @DubboReference
    private AdminRoleService adminRoleService;

    /**
     * @Description 分页查询用户信息
     * @param filters:
     * @param model:
     * @return: java.lang.String
     */
    @RequestMapping
    public String findByPage(@RequestParam Map<String, Object> filters, Model model){
        PageInfo<Admin> page = adminService.findByPage(filters);
        model.addAttribute("page",page);
        model.addAttribute("filters",filters);
        return toIndex("admin");
    }

    /**
     * @Description 跳转到添加页面
     * @return: java.lang.String
     */
    @RequestMapping("/create")
    public String create(){
        return "admin/create";
    }

    /**
     * @Description 添加用户信息
     * @param admin:
     * @param model:
     * @return: java.lang.String
     */
    @RequestMapping("/save")
    public String insert(Admin admin,Model model){
        admin.setPassword(new BCryptPasswordEncoder().encode(admin.getPassword()));
        adminService.insert(admin);
        return successPage(model,"添加用户信息成功!");
    }

    /**
     * @Description 跳转到修改页面
     * @param model:
     * @param id:
     * @return: java.lang.String
     */
    @RequestMapping("/edit/{id}")
    public String getById(Model model, @PathVariable Long id){
        Admin admin = adminService.getById(id);
        model.addAttribute("admin",admin);
        return "admin/edit";
    }

    /**
     * @Description 修改用户信息
     * @param model:
     * @param admin:
     * @return: java.lang.String
     */
    @RequestMapping("/update")
    public String update(Model model,Admin admin){
        adminService.update(admin);
        return successPage(model,"修改用户信息成功!");
    }

    /**
     * @Description 删除用户信息
     * @param id:
     * @return: java.lang.String
     */
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        adminService.delete(id);
        return "redirect:/admin";
    }

    @RequestMapping("/uploadShow/{id}")
    public String uploadShow(@PathVariable Long id,Model model){
        model.addAttribute("id",id);
        return "admin/upload";
    }

    /**
     * @Description 上传头像
     * @return: java.lang.String
     */
    @RequestMapping("/upload/{id}")
    public String upload(@PathVariable Long id, MultipartFile file,Model model) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName = UUID.randomUUID().toString() + suffix;
        Admin admin = new Admin();
        admin.setId(id);
        admin.setHeadUrl("http://riyliglse.hn-bkt.clouddn.com/"+fileName);
        adminService.update(admin);
        QiniuUtils.upload2Qiniu(file.getBytes(),fileName);

        return successPage(model,"上传头像成功!");
    }

    @RequestMapping("/assignShow/{adminId}")
    public String assignShow(@PathVariable Long adminId,Model model){
        model.addAttribute("adminId", adminId);
        Map<String, List<Role>> roleMap = roleService.findRoleListById(adminId);
        model.addAllAttributes(roleMap);
        return "admin/assignShow";
    }

    @RequestMapping("/assignRole")
    public String assignRole(Model model,Long adminId,@RequestParam("roleIds") List<Long> roleIds){
        adminRoleService.assignRole(adminId,roleIds);
        return successPage(model,"分配角色成功!");
    }
}
